package com.caiyouxi.boss.buser.service.impl;

import com.caiyouxi.boss.buser.mapper.SysDepartmentsMapper;
import com.caiyouxi.boss.buser.model.SysDepartmentsBean;
import com.caiyouxi.boss.buser.service.SysDepartmentsService;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/10/25.
 */
@Service
public class SysDepartmentsServiceImpl extends BaseService implements SysDepartmentsService {

    private Logger logger = Logger.getLogger(SysDepartmentsServiceImpl.class);

    @Autowired
    private SysDepartmentsMapper sysDepartmentsMapper;


    //修改部门信息
    @Override
    public ResData update_departments(SysDepartmentsBean bean) {
        int name_exists = sysDepartmentsMapper.update_user_is_exist(bean);
        if(name_exists > 0){
            logger.info("存在与自己相同的部门名称");
            return failed(ErrorCode.SYS_DEPARTMENTS_NAME.getCode(),ErrorCode.SYS_DEPARTMENTS_NAME.getMsg(),null);
        }
        int result = sysDepartmentsMapper.update_department(bean);
        if(result == 1 ){
            return success(null);
        }
        return failed(ErrorCode.SYS_DEPARTMENTS_UPDATE_ERROR.getCode(),ErrorCode.SYS_DEPARTMENTS_UPDATE_ERROR.getMsg(),null);
    }

    //添加部门信息
    @Override
    public ResData add_departments(SysDepartmentsBean bean) {

        int name_exist = sysDepartmentsMapper.add_user_is_exist(bean);
        if(name_exist > 0){
            logger.info("用户已经存在");
            return failed(ErrorCode.SYS_DEPARTMENTS_NAME.getCode(),ErrorCode.SYS_DEPARTMENTS_NAME.getMsg(),null);
        }
        int result = sysDepartmentsMapper.insert_department(bean);
        if(result == 1 ){
            return success(null);
        }
        return failed(ErrorCode.SYS_DEPARTMENTS_UPDATE_ERROR.getCode(),ErrorCode.SYS_DEPARTMENTS_UPDATE_ERROR.getMsg(),null);
    }

    //查询所有部门信息
    @Override
    public ResData get_all_departments(SysDepartmentsBean bean) {

        logger.info("获取所有部门信息");
        List<SysDepartmentsBean> depart_list = sysDepartmentsMapper.get_all_department(bean);
        return success(depart_list);
    }

    //通过部门id查询部门信息
    @Override
    public ResData get_departments_by_id(SysDepartmentsBean bean) {
        logger.info("通过部门id查询部门信息");
        SysDepartmentsBean sdb_bean = sysDepartmentsMapper.get_departments_by_id(bean);
        return success(sdb_bean);
    }
}
